﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Truron.Purchase.Interface;
using Truron.Purchase.Api.Utility;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Truron.Purchase.Model;
using Newtonsoft.Json;
using Truron.Purchase.Utility;

namespace Truron.Purchase.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class Purchase_SupplyAreaController : ControllerBase
    {
        private readonly IPurchase_SupplyAreaService _purchase_SupplyAreaService;
        public Purchase_SupplyAreaController(IPurchase_SupplyAreaService Purchase_SupplyAreaService)
        {
            _purchase_SupplyAreaService = Purchase_SupplyAreaService;
        }
        [HttpPost]
        public IActionResult GetList(LayPageModel param)
        {
            var userToken = SysCenterServiceHelp.GetUser(HttpContext);
            int pageSize = param.limit, pageIndex = param.page;
            var noteTuple = _purchase_SupplyAreaService.GetList(param.searchKey, pageSize, pageIndex);
            return Ok(JsonConvert.SerializeObject(new ResultData<object>() { data = noteTuple.list, count = noteTuple.count }));
            //return null;
        }
        
        [HttpPost]
        public IActionResult Del(RecParam param)
        {
            if (param.strEntity.Length == 0)
                return Ok(JsonConvert.SerializeObject(new ResultData(ErrorCodeConst.ParameterError)));
            List<int> ids = JsonConvert.DeserializeObject<List<int>>(param.strEntity);
            var userToken = SysCenterServiceHelp.GetUser(HttpContext);
            var resultCode = _purchase_SupplyAreaService.Del(ids, userToken);
            return Ok(JsonConvert.SerializeObject(new ResultData(resultCode)));
        }
        [HttpPost]
        public IActionResult GetAllList(CommonSearchModel param)
        {
            var List = _purchase_SupplyAreaService.GetListAll(param.keyWord);
            return Ok(JsonConvert.SerializeObject(new ResultData<object>() { data = List }));
        }
        [HttpPost]
        public IActionResult Save(RecParam param)
        {
            Purchase_SupplyArea entity = JsonConvert.DeserializeObject<Purchase_SupplyArea>(param.strEntity);
            var userToken = SysCenterServiceHelp.GetUser(HttpContext);
            int resultCode = _purchase_SupplyAreaService.Save(entity, userToken);
            return Ok(JsonConvert.SerializeObject(new ResultData(resultCode)));
        }
        public class RecParam
        {
            public string strEntity { get; set; }
        }

    }
    
}
